﻿# +---------------------------------------------------------------------------
# | File : CommonFunctions.psm1                                          
# | Version : 1.02                                      
# | Purpose : Functions used for application deployments.
# | Synopsis: 
# +---------------------------------------------------------------------------
# | Maintenance History                                            
# | -------------------                                            
# | Name            Date         Version   Description        
# | -------------------------------------------------------------------------- 
# | Brian Sykes     05-02-2014   1.00      Creation - Added Backup function
# | Brian Sykes     05-03-2014   1.01      Created Publish function
# | Brian Sykes     05-17-2014   1.02      Created Write-Log function
# +---------------------------------------------------------------------------


function Backup
{
    <#
    .Parameter Source
        Location of the source files to be backed up
    .Parameter Destination
        Destination folder to store backup
    .Parameter Server
        Hostname (or IP) of Server
    .Return
       [String] Either 'SUCCESS' or error message.
    #>

    Param(
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $Source,
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $Destination,
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $Server
    )

    try
    {   
        # Backup from source to destination
        Copy-item \\$Server\$Source \\$Server\$Destination -ErrorAction Continue -Recurse -Force
    }
    catch [exception]
    {
        # Store exception message and pipeline position to a string variable
        $failMessage = "$($_.Exception.ToString()). $($_.InvocationInfo.PositionMessage)" + "`nFailed to backup files"
        # Return the exception message. Backup function failed. 
        return $failMessage
    }
    # Backup successful
    return 'SUCCESS'
}



function Publish
{
    <#     
    .Parameter SourceFiles          
        Path to the source files to be deployed     
    .Parameter DestDir         
        Destination folder     
    .Parameter Server         
        Hostname (or IP) of Server     
    .Return        
        [String] Either 'SUCCESS' or error message.     
    #>

    Param(
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $SourceFiles,
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $Destination,
    [Parameter(Mandatory=$true)] [ValidateNotNullOrEmpty()]
    [string] $Server
    )

    try
    {        
        # Copy contents of source folder to destination folder 
        Copy-item $SourceFiles \\$Server\$Destination -Recurse -Force -ErrorAction Stop
    }
    catch [exception]
    {
        # Capture exception message and position in pipeline as string
        $failMessage = "$($_.Exception.ToString()). $($_.InvocationInfo.PositionMessage)" + "`nFailed to publish files"
        # Return exception message. Publish failed.
        return $failMessage
    }
    # Publish successful
    return 'SUCCESS'
}


function Write-Log
{
    <#
    .Description
        Writes to log file, with option to echo output to console
    .Parameter Message          
        Message to be logged   
    .Parameter Path         
        Path to log file     
    .Parameter EchoHost         
        If added will echo to console  
    #>

    [cmdletbinding()]
    Param(
	[Parameter(ValueFromPipeline=$true,Mandatory=$true)] [ValidateNotNullOrEmpty()]
	[string] $Message,
	[Parameter()]
	[IO.FileInfo] $Path = ”$env:temp\PS_Log.txt”,	
    [Parameter()] [ValidateSet(“True”, “False”)]
	[string] $EchoHost = "False"
    )

    try
    {
        # Check for existence of log file
        if(-not(Test-Path $Path))
        {
            # Create log file
            New-Item -Path $Path -ItemType File -Force
        }

        # Create string for logging
        $msg = (Get-Date -Format “yyyy-MM-dd HH:mm:ss”) + " >> $Message"

        # Write to log
        $msg | Out-File -FilePath $Path -Append
        
        # If Debug mode, write to console as well
        if($EchoHost -eq $true){
            Write-Host $msg
        }
    }
    catch [exception]
    {
        $failMessage = "$($_.Exception.ToString()). $($_.InvocationInfo.PositionMessage)" + "`nLogging failure"
        throw $failMessage
    }
}